package com.ruoyi.system.controller;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Interview;
import com.ruoyi.system.service.IInterviewService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 面试记录Controller
 * 
 * @author ruoyi
 * @date 2023-11-14
 */
@RestController
@RequestMapping("/system/interview")
public class InterviewController extends BaseController
{
    @Autowired
    private IInterviewService interviewService;


    /**
     * 获取一周的面试数量
     * @return
     */
    @Anonymous
    @GetMapping("/oneWeekInterviewNum")
    public AjaxResult getOneWeekInterviewNum(){
        // 获取所有的面试
        List<Interview> interviews = interviewService.selectInterviewList(null);

        // 构建数组形式的数据
        Map<LocalDate, Integer> resultMap = new HashMap<>();
        for (Interview in : interviews) {
            // 先获取时间
            Date time = in.getTime();
            // 将time格式化成 yyyy-mm-dd 形式
            LocalDate localDate = time.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            // 更新面试数量
            resultMap.put(localDate, resultMap.getOrDefault(localDate, 0) + 1);
        }

        // 转换为对象数组形式
        List<Map<String, Object>> result = new ArrayList<>();
        resultMap.forEach((date, count) -> {
            Map<String, Object> item = new HashMap<>();
            item.put("date", date.toString());
            item.put("count", count);
            result.add(item);
        });

        return AjaxResult.success(result);
    }

    /**
     * 查询面试记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:interview:list')")
    @GetMapping("/list")
    public TableDataInfo list(Interview interview)
    {
        startPage();
        List<Interview> list = interviewService.selectInterviewList(interview);
        return getDataTable(list);
    }

    /**
     * 导出面试记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:interview:export')")
    @Log(title = "面试记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Interview interview)
    {
        List<Interview> list = interviewService.selectInterviewList(interview);
        ExcelUtil<Interview> util = new ExcelUtil<Interview>(Interview.class);
        util.exportExcel(response, list, "面试记录数据");
    }

    /**
     * 获取面试记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:interview:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(interviewService.selectInterviewById(id));
    }

    /**
     * 新增面试记录
     */
    @PreAuthorize("@ss.hasPermi('system:interview:add')")
    @Log(title = "面试记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Interview interview)
    {
        return toAjax(interviewService.insertInterview(interview));
    }

    /**
     * 修改面试记录
     */
    @PreAuthorize("@ss.hasPermi('system:interview:edit')")
    @Log(title = "面试记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Interview interview)
    {
        return toAjax(interviewService.updateInterview(interview));
    }

    /**
     * 删除面试记录
     */
    @PreAuthorize("@ss.hasPermi('system:interview:remove')")
    @Log(title = "面试记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(interviewService.deleteInterviewByIds(ids));
    }
}
